home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / system / flicb_1.6 / flicb_1 / flicb / strlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-08  |  2.9 KB  |  173 lines

  1. /* Copyright (c) 1990 by Carrick Sean Casey. */
  2. /* For copying and distribution information, see the file COPYING. */
  3.  
  4. /* Modified by Mark Giaquinto for International CB, Copyright (C) 1991 */
  5. /* Slightly modified by Mark Luljak for FLICB */
  6. /* routines to maintain a generic list of strings */
  7.  
  8. #include "flicb.h"
  9.  
  10.  
  11. STRLIST *
  12. strmakenode(size)
  13. int size;
  14. {
  15.     STRLIST *p;
  16.     if ((p = (STRLIST *) malloc(sizeof(STRLIST) + size)) == NULL)
  17.         return(NULL);
  18.     p->next = NULL;
  19.     p->prev = NULL;
  20.     *p->str = '\0';
  21.     return(p);
  22. }
  23.  
  24. /* link node s to the head of the list */
  25.  
  26. strlinkhead(s, head, tail)
  27. STRLIST *s, **head, **tail;
  28. {
  29.     if (*tail == 0) {
  30.         s->prev = s->next = 0;
  31.         *head = *tail = s;
  32.     } else {
  33.         (*head)->prev = s;
  34.         s->prev = 0;
  35.         s->next = *head;
  36.         *head = s;
  37.     }
  38. }
  39.  
  40.  
  41. /* link node s to the tail of the list */
  42.  
  43. strlinktail(s, head, tail)
  44. STRLIST *s, **head, **tail;
  45. {
  46.     if (*head == 0) {
  47.         s->prev = s->next = 0;
  48.         *head = *tail = s;
  49.     } else {
  50.         (*tail)->next = s;
  51.         s->prev = *tail;
  52.         s->next = 0;
  53.         *tail = s;
  54.     }
  55. }
  56.  
  57.  
  58. /* link node s in after node i */
  59. /* node i must be defined */
  60.  
  61. strlinkafter(s, i, head, tail)
  62. STRLIST *s, *i, **head, **tail;
  63. {
  64.     s->prev = i;
  65.     s->next = i->next;
  66.     i->next = s;
  67.     if (i == *tail)
  68.         *tail = s;
  69.     else
  70.         s->next->prev = s;
  71. }
  72.  
  73.  
  74. /* link node s in before node i */
  75. /* node i must be defined */
  76.  
  77. strlinkbefore(s, i, head, tail)
  78. STRLIST *s, *i, **head, **tail;
  79. {
  80.     s->prev = i->prev;
  81.     s->next = i;
  82.     i->prev = s;
  83.     s->prev->next = s;
  84. }
  85.  
  86.  
  87. /* unlink node s */
  88.  
  89. strunlink(s, head, tail)
  90. STRLIST *s, **head, **tail;
  91. {
  92.     if (s->prev == 0)
  93.         if (s->next) {
  94.             *head = s->next;
  95.             s->next->prev = 0;
  96.         } else
  97.             *tail = *head = 0;
  98.     else if (s->next == 0)
  99.         if (s->prev) {
  100.             *tail = s->prev;
  101.             s->prev->next = 0;
  102.         } else
  103.             *tail = *head = 0;
  104.     else {
  105.         s->prev->next = s->next;
  106.         s->next->prev = s->prev;
  107.     }
  108. }
  109.  
  110. /* link s into the list such that it is inserted in alphabetical order */
  111. /* if caseindep != 0, it is done case independently */
  112.  
  113. strlinkalpha(s, head, tail, caseindep)
  114. STRLIST *s, **head, **tail;
  115. int caseindep;
  116. {
  117.     STRLIST *i;
  118.  
  119.     /* link into head if list is empty */
  120.     if (!*head)
  121.         strlinkhead(s, head, tail);
  122.  
  123.     /* otherwise, alphabetize */
  124.     else {
  125.         /* find insertion point */
  126.         for (i = *head; i; i = i->next) {
  127.             if (caseindep) {
  128.                 if (strcasecmp(s->str, i->str) < 0)
  129.                     break;
  130.             } else
  131.                 if (strcmp(s->str, i->str) < 0)
  132.                     break;
  133.         }
  134.  
  135.         /* see what we found */
  136.         if (i) {
  137.             if (i == *head)
  138.                 strlinkhead(s, head, tail);
  139.             else
  140.                 strlinkbefore(s, i, head, tail);
  141.         } else
  142.             strlinktail(s, head, tail);
  143.     }
  144. }
  145.  
  146.  
  147. /* get the node in a STRLIST containing str */
  148. /* if caseindep != 0, the searching is done case insensitive */
  149. /* returns pointer on success, 0 on failure */
  150.  
  151. STRLIST *
  152. strgetnode(str, head, caseindep)
  153. char *str;
  154. STRLIST *head;
  155. int caseindep;
  156. {
  157.     STRLIST *p;
  158.  
  159.     for (p = head; p; p = p->next) {
  160.         if (caseindep) {
  161.             if (!strcasecmp(str, p->str))
  162.                 break;
  163.         } else
  164.             if (!strcmp(str, p->str))
  165.                 break;
  166.     }
  167.  
  168.     if (p)
  169.         return(p);
  170.     else
  171.         return(0);
  172. }
  173.